package com.example.controller.admin;

import com.example.constant.JwtClaimsConstant;
import com.example.dto.EmployeeLoginDTO;
import com.example.properties.JwtProperties;
import com.example.result.PageBeanResult;
import com.example.result.Result;
import com.example.dto.EmployeeDTO;
import com.example.service.EmployeeService;
import com.example.utils.JwtUtil;
import com.example.vo.EmployeeLoginVO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@Slf4j
@Tag(name="员工管理")
@RequestMapping("/admin/employee")
public class EmployeeController {

    @Autowired
    private EmployeeService employeeService;

    @Autowired
    private JwtProperties jwtProperties;

    /**
     * 员工登陆 生成jwt令牌
     * @param employeeLoginDTO
     * @return
     */
    @PostMapping("/login")
    @Operation(summary = "员工登陆")
    public Result login(@RequestBody EmployeeLoginDTO employeeLoginDTO)  {
        log.info("员工登录：{}", employeeLoginDTO);

        EmployeeDTO employeeDTO = employeeService.login(employeeLoginDTO);

        //登录成功后，生成jwt令牌
        Map<String, Object> claims = new HashMap<>();
        claims.put(JwtClaimsConstant.EMP_ID, employeeDTO.getId());
        String token = JwtUtil.createJWT(
                jwtProperties.getAdminSecretKey(),
                jwtProperties.getAdminTtl(),
                claims);

        EmployeeLoginVO employeeLoginVO = EmployeeLoginVO.builder()
                .id(employeeDTO.getId())
                .userName(employeeDTO.getUsername())
                .name(employeeDTO.getName())
                .token(token)
                .build();

        return Result.success(employeeLoginVO);
    }
    /**
     * 退出
     *
     * @return
     */
    @PostMapping("/logout")
    @Operation(summary = "员工退出")
    public Result logout() {

        return Result.success();
    }


    /**
     * 展示所有员工信息
     * @return emplist
     */
    @GetMapping("/pages")
    @Operation(summary = "展示所有员工信息")
    public Result showEmployee(@RequestParam(defaultValue ="0") Integer page,
                               @RequestParam(defaultValue ="10") Integer pageSize){

        log.info("展示所有员工信息:");
        PageBeanResult emplist=employeeService.showEmployee(page,pageSize);
        return Result.success(emplist);

    }
    /**
     * 根据删除员工
     */
    @DeleteMapping("/emps/{id}")
    @Operation(summary = "根据id删除员工")
    public Result deleteEmployee(@PathVariable Integer id){
        log.info("根据id删除员工:{}",id);
        employeeService.deleteEmployee(id);
        return Result.success();

    }
    /**
     * 新增员工
     */
    @PostMapping
    @Operation(summary = "新增员工")
    public Result addEmployee(@RequestBody EmployeeDTO emp){
        employeeService.addEmployee(emp);
        return Result.success();
    }
    /**
     * 按姓名查询员工并分页展示
     */
    @GetMapping("/page")
    @Operation(summary = "按姓名查询员工并分页展示")
    public Result selectEmployee(String name,@RequestParam(defaultValue ="0") Integer page, @RequestParam(defaultValue ="10") Integer pageSize){
        PageBeanResult pageResult =employeeService.selectEmployee(name,page,pageSize);
        log.info("按名字模糊查询员工，并分页展示。{},{},{}",name,page,pageSize);
        return  Result.success(pageResult);
    }


    /**
     * 根据id更改员工状态 1为启用 0为禁用
     */
    @PostMapping("/status/{status}")
    @Operation(summary = "根据id更改员工状态")
    public Result  starOrStop(@PathVariable Integer status ,Integer id){
        employeeService.starOrStop(status,id);
        return Result.success();
    }

    /**
     * 修改员工数据回显 根据id
     */
    @GetMapping("/{id}")
    @Operation(summary = "修改员工数据回显")
    public Result getById(@PathVariable  Integer id){

        EmployeeDTO employeeDTO=employeeService.getById(id);
        return Result.success(employeeDTO);
    }
    /**
     * 修改员工
     */

    @PutMapping("/employee")
    @Operation(summary ="修改员工信息")
    public Result updateEmployee(@RequestBody EmployeeDTO employeeDTO){
        employeeService.updateEmployee(employeeDTO);
        return Result.success();
    }
    /**
     * 修改密码
     */
    @PutMapping("/editPassword")
    @Operation(summary = "修改密码")
    public Result changePassWord(String pwd,Integer id){
        employeeService.changePassWord(pwd,id);
        return Result.success();

    }
    /**
     * 批量删除员工
     */
    @DeleteMapping("/empss/{ids}")
    @Operation(summary = "批量删除员工")
    public Result deleteEmps(@PathVariable List<Integer> ids){
        log.info("批量删除员工:{}",ids);
        employeeService.deleteEmps(ids);
        return Result.success();
    }
}
